package com.snap.vseries.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.snap.vseries.AppConfig;
import com.snap.vseries.R;
import com.snap.vseries.bean.ChannelBean;
import com.snap.vseries.bean.ExpInfo;

public class ResultTableListAdapter extends RecyclerView.Adapter<ResultTableListAdapter.Holder> {

    private ExpInfo experimentInfo;

    public void initData(ExpInfo experimentInfo) {
        this.experimentInfo = experimentInfo;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new Holder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_result_table_list, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        if (experimentInfo == null || experimentInfo.getHoleInfo() == null) {
            return;
        }
        SparseArray<ChannelBean> ctBeans = experimentInfo.getHoleInfo().getChannelData();
        int channelIndex = ctBeans.keyAt(position) / AppConfig.TOTAL_HOLE_COUNT;
        holder.channelTv.setText(AppConfig.CHANNEL_NAMES[channelIndex]);
        ChannelBean ctBean = ctBeans.get(channelIndex);
        float ct = ctBean.getCtValue();
        holder.ctTv.setText(ct == 0f ? "N/A" : String.valueOf(ct));
        // TODO: 2020/4/2  阳性为+ 阴性为-
        if (ctBean.getCtValue() == 0) {
            holder.conclusionTv.setTextColor(0xFFC1C3CF);
            holder.conclusionTv.setText("-");
        } else {
            holder.conclusionTv.setTextColor(0xFF568BFF);
            holder.conclusionTv.setText("+");
        }
    }

    @Override
    public int getItemCount() {
        if (experimentInfo == null || experimentInfo.getHoleInfo() == null) {
            return 0;
        }
        SparseArray<ChannelBean> ctBeans = experimentInfo.getHoleInfo().getChannelData();
        return ctBeans == null ? 0 : ctBeans.size();
    }

    public void changeAnalyzeType() {
        notifyDataSetChanged();
    }

    static class Holder extends RecyclerView.ViewHolder {


        TextView channelTv;
        TextView ctTv;
        TextView conclusionTv;

        public Holder(View itemView) {
            super(itemView);
            channelTv = itemView.findViewById(R.id.channel_tv);
            ctTv = itemView.findViewById(R.id.ct_tv);
            conclusionTv = itemView.findViewById(R.id.conclusion_tv);
        }
    }
}
